package presentation;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domain.EmpresaPrivada;
import domain.EmpresaPublica;

/**
 * Servlet implementation class for Servlet: FrontController
 *
 */
 public class FrontController extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public FrontController() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
		
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
		
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (logado(request)) {
			String resource = request.getParameter("res");
			RequestDispatcher d = request.getRequestDispatcher(resource);
			if(d!=null){
				d.forward(request,response);
			}
			//System.out.println("LOGADO ACCEDIENDO A " + resource);
		} else {
			
			response.sendRedirect("./error/error.jsp");
		}
	}

	
	
	private boolean logado(HttpServletRequest request){
		boolean logado = false;

		HttpSession session = request.getSession(false);

		String cif = request.getParameter("cif");
		String pass = request.getParameter("pass");
		String pub = request.getParameter("pub");
		
		
		
		//--------------------------------
		//System.out.println(cif+"  "+pass+"  "+pub);
		if(pub == null){
			pub="false";			
			
		}else if(pub.equals("on")){
			
			pub="true";
		}
		request.setAttribute("pub", pub);
		/*Esta chapuza se debe a que cuando pub tiene un valor
		 * diferente de true no lo pilla del request obteniéndose null
		 * Si a vosotros os funciona comentad el if*/
		//--------------------------------
		

		if (session == null) {
			session = request.getSession();
			//System.out.println("uno");
			
			if (cif == null || pass == null
					|| cif.length() == 0 || pass.length() == 0 || pub==null) {
				logado = false;
				

			} else {
				if (valido(cif,pass,pub)) {
					logado = true;
					session.setAttribute("session.cif", cif);
				} else {
					logado = false;
					
				}

			}

		} else {
			//System.out.println("dos");
			if (cif == null || pass == null || pub ==null) {
				logado = true;
			} else {
				if (valido(cif, pass,pub)) {
					logado = true;				
					session.setAttribute("session.cif", cif);
				} else {
					logado = false;
				}

			}
		}
		
		return logado;
		
	}
	
	private boolean valido(String cif,String pass,String pub){
		
		if (pub.equals("true")) {
			return accesoEntidadPublica(cif,pass);
		} else {
			return accesoEntidadPrivada(cif,pass);
		}
	}
	
	private boolean accesoEntidadPublica(String cif, String pass) {
		EmpresaPublica ep= new EmpresaPublica();
		return ep.login(cif,pass);
	}

	private boolean accesoEntidadPrivada(String cif, String pass){
		EmpresaPrivada epri= new EmpresaPrivada();
		return epri.login(cif,pass);
	}
    
}